package solver;

import model.Problem;
import model.Solution;
import model.SolverEnvironment;

public class HeuristicSolver extends BaseSolver {

	public HeuristicSolver(SolverEnvironment env) {
		super(env);
	}
	
	public HeuristicSolver(Problem problem, Solution initial_solution,
			Solution current_solution) {
		super(problem, initial_solution, current_solution);
	}

	public HeuristicSolver(Problem problem, Solution initial_solution) {
		super(problem, initial_solution);
	}

	public void solve() {
		Problem.VERBOSE = false;
		long cost, best_cost = Long.MAX_VALUE;
		int tries = 0;
		while (tries < 10000) {
			int[][] moves = generateRandomMoves(2);
			setPotentialSolution(getInitialSolution().copy());
			getPotentialSolution().makeMoves(moves);
			getFullValidator().setSolutionPair(getInitialSolution(), getPotentialSolution());
			if (getFullValidator().isFeasible()) {
				getFullValidator().computeCost();
				cost = getFullValidator().getNewSolutionCost();
				// ce.printCostAnalysis();
				if (cost < best_cost) {
					System.out.printf("Better cost found %d from %d\n", cost,
							best_cost);
					best_cost = cost;
					tries = 0;
					setCurrentSolution(getPotentialSolution().copy());
				}
			}
			tries++;
		}
		System.out.println("############################################");
		getFullValidator().setSolutionPair(getInitialSolution(), getCurrentSolution());
		getFullValidator().computeCost();
		getFullValidator().printCostAnalysis();
		System.out.println("Best solution cost = " + best_cost);
		Problem.VERBOSE = true;
	}

	public Solution getNewSolution() {
		return getCurrentSolution();
	}

	@Override
	public int solveWithTimeLimit(int seconds) {
		//TODO Fix this
		solve();
		return 0;
	}

	}
